package com.sky.service.user.impl;

import com.sky.context.BaseContext;

import com.sky.exception.ShopIDNotNULLException;
import com.sky.mapper.user.MerchatMapper;
import com.sky.mapper.user.AddressBookMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.pojo.AddressBook;
import com.sky.pojo.Merchant;
import com.sky.result.Result;
import com.sky.service.user.AddressBookService;
import com.sky.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private  ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private  MerchatMapper merchantMapper;
    /**
     * 条件查询
     *
     * @param addressBook
     * @return
     */
    public List<AddressBook> list(AddressBook addressBook) {
        return addressBookMapper.list(addressBook);
    }

    /**
     * 新增地址
     *
     * @param addressBook
     */
    public void save(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.insert(addressBook);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public AddressBook getById(Long id) {
        AddressBook addressBook = addressBookMapper.getById(id);
        return addressBook;
    }

    /**
     * 根据id修改地址
     *
     * @param addressBook
     */
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     */
    @Transactional
    public void setDefault(AddressBook addressBook) {
        //1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ?
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateIsDefaultByUserId(addressBook);

        //2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据id删除地址
     *
     * @param id
     */
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }
    @Override
    public Result<String> getgetEstimatedDeliveryTime(Integer shopId, String customerAddress) {

        //获取所有菜品的制作时间
        Long currentId = BaseContext.getCurrentId();
        Integer makeTimeCost = shoppingCartMapper.getMakeTimeByUserId(currentId);

        //计算配送地址和商家地址之间的配送时间
        Merchant merchant = merchantMapper.search(shopId);
        if(merchant==null){
            throw new ShopIDNotNULLException("找不到商家信息");
        }
        //工具类，直接使用
        Integer towPointCost = MapUtils.baoiduTwoPointCostTime(merchant.getShopAddress(), customerAddress);

        //预计送达时间 = 当前时间+菜品制作时间+配送时间+预留10分钟
        Integer allCost = makeTimeCost*60+towPointCost+600;
        LocalDateTime estimatedDeliveryTime = LocalDateTime.now().plusSeconds(allCost);
        return Result.success(estimatedDeliveryTime.toString());
    }

}